package org.xxpay.pay.channel.tfpay;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.common.domain.api.AbstractRes;
import org.xxpay.core.common.domain.api.ApiBuilder;
import org.xxpay.core.common.domain.api.PayRes;
import org.xxpay.core.common.domain.api.msg.QueryRetMsg;
import org.xxpay.core.common.util.MyBase64;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.common.vo.DBApplicationConfig;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.pay.channel.BasePayment;
import org.xxpay.pay.mq.BaseNotify4MchPay;

/**
 * @author: pangxiaoyu
 * @date: 26/04/20
 * @description: 支付宝转账接口
 */
@Service
public class TfpayPaymentService extends BasePayment {

    @Autowired
    public BaseNotify4MchPay baseNotify4MchPay;

    private static final MyLog _log = MyLog.getLog(TfpayPaymentService.class);

    @Override
    public String getChannelName() {
        return TfpayConfig.CHANNEL_NAME;
    }

    /**
     * 支付
     * @param payOrder
     * @return
     */
    @Override
    public AbstractRes pay(PayOrder payOrder) {
        String channelId = payOrder.getChannelId();
        AbstractRes retObj;
        switch (channelId) {
            case TfpayConfig.CHANNEL_NAME_TFPAY_ALIPAY :
                retObj = doPay4Cc(payOrder, null, null);
                break;
            default: return ApiBuilder.bizError("不支持的渠道[channelId="+channelId+"]");
        }
        return retObj;
    }

    /**
     * 查询订单
     * @param payOrder
     * @return
     */
    @Override
    public QueryRetMsg query(PayOrder payOrder) {
        // 上游没有查询接口，直接返回
        return QueryRetMsg.waiting();
    }


    /**
     * 跳转到复制账号页面
     * @param payOrder
     * @return
     */
    public  AbstractRes doPay4Cc(PayOrder payOrder, String type, String ccType) {
        TfpayConfig tfpayConfig = new TfpayConfig(getPayParam(payOrder));
        PayRes retObj = ApiBuilder.buildSuccess(PayRes.class);
        String account = tfpayConfig.getAccount();                  // 账号
        String accountName = tfpayConfig.getAccountName();          // 姓名
        try {
            int updateCount = commonService.payOrderService.updateStatus4Ing(payOrder.getPayOrderId(), null);
            _log.info("[{}]更新订单状态为支付中:payOrderId={},result={}", getChannelName(), payOrder.getPayOrderId(), updateCount);
            // 获取配置路由
            DBApplicationConfig dbConfig = getDBApplicationConfig();
            String payJumpUrl;
            StringBuffer payForm = new StringBuffer();
            // 将参数封装在json并做base64传递
            JSONObject paramObj = new JSONObject();
            paramObj.put("account", account);
            paramObj.put("payOrderId", payOrder.getPayOrderId());
            paramObj.put("accountName", accountName);
            paramObj.put("amount", payOrder.getAmount());

            String param = MyBase64.encode(paramObj.toJSONString().getBytes());
            String toPayUrl = dbConfig.getConfig4PayUrl() + "/channel/"+TfpayConfig.CHANNEL_NAME + "/" + param;

            payForm.append("<form style=\"display: none\" action=\""+toPayUrl+"\" method=\"post\">");
            payForm.append("<input type=\"submit\" value=\"立即支付\" style=\"display:none\" >");
            payForm.append("</form>");
            payForm.append("<script>document.forms[0].submit();</script>");
            // 支付链接地址
            payJumpUrl = toPayUrl + "?param=" + param;
            retObj.setPayOrderId(payOrder.getPayOrderId()); // 设置支付订单ID
            JSONObject payParams = new JSONObject();
            payParams.put("payMethod", PayConstant.PAY_METHOD_FORM_JUMP);
            payParams.put("payUrl", payForm);
            payParams.put("payJumpUrl", payJumpUrl);
            retObj.setPayUrl(payForm.toString());
            retObj.setPayMethod(PayConstant.PAY_METHOD_FORM_JUMP);
            return retObj;
        } catch (Exception e) {
            _log.error(e, "");
            return ApiBuilder.bizError("操作失败!");
        }
    }

}
